// pages/fyx/fyx.js
var common = require('../../utils/common.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fylist: [],//费用列表
    feeItemType:"",//费用项类型
  },

  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);

    var checkboxItems = this.data.fylist, values = e.detail.value;

    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems[i].itemName == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      fylist: checkboxItems
    });

  },
  backTjht: function () {
    var chooseFylist = [];
    for (var i = 0; i < this.data.fylist.length; i++) {
      if (this.data.fylist[i].checked == true) {
        chooseFylist.push(this.data.fylist[i]);
      }
    }
    // app.globalData.fylist = app.globalData.fylist.concat(chooseFylist);
    wx.setStorageSync("feeList", chooseFylist);
    wx.navigateBack({
      delta: 1
    })
  },

//初始化数据
  initData:function(){
    var that = this;
    // var url = "/wx/contract/initContractPage";
    var url = "/wx/dict/list";
    var data = { key:this.data.feeItemType};
    console.log(data)
    var success = function (res) {
      console.log(res.data);
      // var yjdata = res.data.data.foregiftList;
      // var yjdata = [{ "id": 1, "key": "2", "value": "房屋押金", "pid": 1, "sequence": 1 }];
      var change = {
        "id": "id",
        "key": "itemKey",
        "value": "itemName",
        "pid": "pid",
        "sequence": "sequence"
      }
      var fydata = res.data.data;
      if (fydata){
        for (var i = 0; i < fydata.length; i++) {
          // fydata[i]["chargetype"]=1;
          fydata[i].id="";
          that.replace(fydata[i], change);
        }

        // for (var i = 0; i < yjdata.length; i++) {
        //   that.replace(yjdata[i], change);
        // }
        console.log('11111');
        console.log(fydata);
        that.setData({
          fylist: fydata,
        })
        
        for (var i = 0; i < that.data.fylist.length; i++) {
          if (that.data.fylist[i].itemName == "水费" || that.data.fylist[i].itemName=="电费"){
            that.data.fylist[i]["chargeType"] = 0;
          }else{
            that.data.fylist[i]["chargeType"] = 1;
          }
         
        }
        console.log(that.data.fylist);
      }
    };

    common._get(url, data, success)
  },
  //换key
  replace: function (target, change) {
    for (var key in target) {
      target[change[key]] = target[key];
      if (change[key] != key) {
        delete target[key];
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.feeItemType = options.key;
    console.log(this.data.feeItemType)
    // var fylist = JSON.parse(options.fylist);
    // this.setData({
    //   fylist: fylist
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})